package com.cms.work.controller.sys;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.cms.work.exception.ExceptionCodeEnum;
import com.cms.work.exception.SystemException;

@Controller
public class LoginController {
	
    @RequestMapping(value="/login", method={RequestMethod.POST, RequestMethod.GET})
    public String login(HttpServletRequest request, HttpServletResponse response) {
        String exceptionClassName = (String) request.getAttribute("shiroLoginFailure");
        if(exceptionClassName != null){
            if(UnknownAccountException.class.getName().equals(exceptionClassName)){
                throw new SystemException(ExceptionCodeEnum.SYS_USER_NOEXIST);
            }else if(IncorrectCredentialsException.class.getName().equals(exceptionClassName)){
                throw new SystemException(ExceptionCodeEnum.SYS_USER_ISERROR);
            }else if(DisabledAccountException.class.getName().equals(exceptionClassName)){
            	throw new SystemException(ExceptionCodeEnum.SYS_USER_ISLOCK);
            }else{
                throw new SystemException(ExceptionCodeEnum.SYS_USER_UNKNOWN);
            }
        }
        return "login";
    }
    
}
